package com.haidechizi.leetcode;

public class _45_跳跃游戏II {
    public int jump(int[] nums) {
        if (nums == null || nums.length == 1) {
            return 0;
        }
        int maxIndex = 0;
        int currentIndex = 0;
        int count = 0;
        while (currentIndex < nums.length - 1 && currentIndex <= maxIndex + nums[maxIndex]) {
            if (maxIndex + nums[maxIndex] >= nums.length - 1) {
                break;
            }
            // 获取最大的值
            int max = 0;
            while (currentIndex < nums.length - 1 && currentIndex <= maxIndex + nums[maxIndex]) {
                if (currentIndex + nums[currentIndex] > max + nums[max]) {
                    max = currentIndex;
                }
                currentIndex++;
            }
            if (max + nums[max] > maxIndex + nums[maxIndex]) {
                maxIndex = max;
                count++;
            }

        }
        count++;
        return count;
    }
}
